﻿using chihuopig.IService;
using System;
using System.Collections.Generic;
using System.Text;
using chihuopig.DTO;
using chihuopig.Model;
using System.Threading.Tasks;
using SqlSugar;

namespace chihuopig.Service
{
    public class AdminMenuService :DBService, IAdminMenuService
    {
        public Task<int> delAll(long[] Id)
        {
            return Task.Factory.StartNew<int>(s => {
                string ls = string.Join(",", (long[])s);
                string sql = $"update  AdminMenu set IsDelete='1' where id in ({ls})";
                return
                Sqlclient.Ado
               .ExecuteCommand(sql);
            }, Id);
        }

        public Task<int> Deleteable(long Id)
        {
            return Sqlclient
                     .Updateable<AdminMenu>()
                     .UpdateColumns(it => new AdminMenu() { IsDelete = true })
                     .Where(c => c.Id == Id)
                     .ExecuteCommandAsync();
        }

        public Task<AdminMenu> EditAsync(long Id)
        {
            return Sqlclient.Queryable<AdminMenu>().Where(s => s.Id == Id).FirstAsync();
        }

        public Task<List<AdminMenu>> GetAdminMenu()
        {
            return Sqlclient.Queryable<AdminMenu>().ToListAsync();
        }

        public Task<List<AdminMenuDTO>> GetadminMenuById(long AdminId)
        {
          
          return  Task.Factory.StartNew(s =>
            {
                List<AdminMenuDTO> list = new List<AdminMenuDTO>();
                AdminMenuDTO model;
                long adminid=(long)s;

                var AmTlist=   Sqlclient.Queryable<AdminMenuType>().ToList();
                foreach (var item in AmTlist)
                {
                    model = new AdminMenuDTO();
                    StringBuilder sqlquer = new StringBuilder();
                    sqlquer.Append($"SELECT a.* FROM AdminMenu a WHERE a.Id in ( SELECT j.MenuId FROM jurisdiction j  WHERE j.Id in ( ");
                    sqlquer.Append($"  SELECT rj.JurisdictionId FROM Role_Jurisdiction rj LEFT JOIN jurisdiction js on js.id=rj.JurisdictionId WHERE rj.roleid in ( 	SELECT ar.RoleId FROM AdminRole  ar ");
                    sqlquer.Append($" LEFT JOIN Admin a on a.id=ar.adminid WHERE a.Id='{adminid}' ) ) ) and ClassId='{item.Id}'");
                    model.AdminMenuTypeName = item.ClassName;
                    model.adminmenuist=Sqlclient.Ado.SqlQuery<AdminMenu>(sqlquer.ToString());
                    list.Add(model);
                }
                return list;

            },AdminId);
        }

        public Task<double> GetPageTotal(int PageSize)
        {
            return Task.Factory.StartNew<double>(s =>
            {
                double size = Convert.ToDouble(s);
                double bnum = Sqlclient
                .Queryable<jurisdiction, AdminMenu>((j, a) => new object[] { JoinType.Left, j.MenuId == a.Id })
                .Where((j, a) => j.IsDelete == false && a.IsDelete == false)
                .OrderBy((j, a) => j.Addtime, SqlSugar.OrderByType.Desc).Count() / size;
                //Math.Ceiling(bnum);
                return Math.Ceiling(bnum);

            }, PageSize);
        }

        public Task<long> Insertable(AdminMenu Model)
        {
            return Sqlclient.Insertable(Model).ExecuteReturnBigIdentityAsync();
        }

        public Task<int> Updateable(AdminMenu Model)
        {
            return Sqlclient.Updateable(Model).ExecuteCommandAsync();
        }
    }
}
